//
//  WDPersonalSettingViewModel.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/17.
//

#import "WDPersonalSettingViewModel.h"

@implementation WDPersonalSettingViewModel

- (void)initialize {
    [super initialize];
    
    self.title = @"个人设置";
    
    ZHCustomUserInfoModel *userInfo = [EDSAppDelegateTool sharedInstance].userInfoModel;
    
    _reqModel = [WDUserInfoUpdateReqModel new];
    _reqModel.id = @(userInfo.id);
    self.reqModel.account = userInfo.account;
    self.reqModel.name = userInfo.name;
    
    TYZCommonCellModel *model = [TYZCommonCellModel new];
    model.title = @"头像";
    model.url =  userInfo.avatar;
//    [self.dataList addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"登录账户";
    model.value = objectNull(userInfo.account);
    model.isCheck = YES;
    [self.dataList addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"姓名";
    model.value = objectNull(userInfo.realName);
    model.placeholder = @"请输入姓名";
    model.isCheck = NO;
    [self.dataList addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"手机号";
    model.value = objectNull(userInfo.phone);
    model.placeholder = @"请输入手机号";
    model.isCheck = YES;
    [self.dataList addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"电子邮件";
    model.isCheck = YES;
    model.value = objectNull(userInfo.email);
    model.placeholder = @"点击输入文本";
    [self.dataList addObject:model];
    
    NSString *birthday = objectNull(userInfo.birthday);
    if (birthday.length > 0) {
        NSArray *list = [birthday componentsSeparatedByString:@" "];
        birthday = [list objectOrNilAtIndex:0];
    }
    model = [TYZCommonCellModel new];
    model.title = @"用户生日";
    model.value = objectNull(birthday);
    model.placeholder = @"选择生日";
    model.isCheck = YES;
    model.isIndicator = YES;
    [self.dataList addObject:model];
    
    
    NSString *str = @"交付中心大家";
    UIFont *font = [UIFont systemFontOfSize:14 weight:UIFontWeightRegular];
    float widthTitle = [str widthForFont:font];
    UIImage *image = [UIImage imageNamed:@"ic_than_more_99"];
    float width = (kScreenW - 20 - image.size.width) - 8 - kRealValue(20) - widthTitle - 2;
    
    model = [TYZCommonCellModel new];
    model.title = @"所属服务站";
    model.value = objectNull(userInfo.stationName);
    float height = [model.value heightForFont:font width:width];
    model.valueHeight = (height > 30 ? height : 30);
    model.cellHeight = 55 - 30 + model.valueHeight;
    model.isCheck = YES;
    [self.dataList addObject:model];
    
    model = [TYZCommonCellModel new];
    model.title = @"所属角色";
    model.value = objectNull(userInfo.roleName);
//    model.value = @"二维热无我认为热无我认为热无温热无若额特特额特特哈哈防护服";
    height = [model.value heightForFont:font width:width];
    model.valueHeight = (height > 30 ? height : 30);
    model.cellHeight = 55 - 30 + model.valueHeight;
    model.isCheck = YES;
    [self.dataList addObject:model];
}

/// 修改用户信息
/// - Parameter completion: completion description
- (void)requestWithUserInfoUpdate:(RequestCompletionBlock)completion {
    @weakify(self);
    // 姓名
    TYZCommonCellModel *model = [self.dataList objectOrNilAtIndex:1];
    self.reqModel.realName = objectNull(model.value);
    
    // 手机号
    model = [self.dataList objectOrNilAtIndex:2];
    self.reqModel.phone = objectNull(model.value);
    
    // 邮箱
    model = [self.dataList objectOrNilAtIndex:3];
    self.reqModel.email = objectNull(model.value);
    
    // 用户生日
    model = [self.dataList objectOrNilAtIndex:4];
    self.reqModel.birthday = objectNull(model.value);
    
    
    
    
    WDUserInfoUpdateRequest *request = [[WDUserInfoUpdateRequest alloc] initWithModel:self.reqModel];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

/// 上传图片
/// - Parameters:
///   - imageModel: imageModel description
///   - completion: completion description
- (void)requestWithUploadImage:(EDSImageVideoModel *)imageModel completion:(RequestCompletionBlock)completion {
    @weakify(self);
    
    WDUploadFileRequest *request = [[WDUploadFileRequest alloc] initWithFiles:@[imageModel]];
    [request startWithCompletionBlockWithSuccess:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        EDSRespondDataEntity *respond = request.responseModelObject;
        if (respond.code == EDS_NETWORK_SUCCESS) {
            WDUploadFileRspModel *rspModel = respond.data;
            self.reqModel.avatar = rspModel.link;
            TYZCommonCellModel *model = self.dataList.firstObject;
            model.url = rspModel.link;
            [EDSAppDelegateTool sharedInstance].userInfoModel.avatar = rspModel.link;
        }
        [self sendResponseObject:request.responseModelObject completion:completion];
    } failure:^(__kindof TYZBaseRequest * _Nonnull request) {
        @strongify(self);
        [self sendResponseObject:request.responseModelObject completion:completion];
    }];
}

@end
